package services;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DataCache {
	public static float _version = (float) 1.03;
	public static Dimension screenDim =	Toolkit.getDefaultToolkit().getScreenSize();
	public static boolean window_locker = false;
	public static Vector<String> vCourseName = new Vector<String>();
	public static Vector<Vector> vCourseCache = new Vector<Vector>();
	public static String[] arrayPodName;
	public static Vector<Vector> vIP = new Vector<Vector>();
	public static Map<String, String> mConfigOption = new HashMap<String, String>();
	public static Color cDarkGreen = new Color(47,100,35);
	public static Date dToday = Calendar.getInstance().getTime();
	public static String sConfigFile = "pb.cfg";
	public static enum eDataSections {CourseMatrix, VAMatrix, PBTemplate, Dictionary, NAM, ROW, Configuration}
	public static String sColumnName[] = {"Country", "Course", "POD", "Student", "Start Date", "Days", "Image", "Instructor"};
	public static String[] sRegion = {
	"Abu Dhabi", "Algeria", "APJ", "AU", "AU - Brisbane", 
	"AU - Canberra", "AU - Melbourne", "AU - Perth", "AU - Sydney", 
	"Australia", "Australia/NZ", "Austria", "Belgium", "Bulgaria", "China", 
	"China TW - Taipei", "China-Onsite", "China/Hong Kong", "China/Onsite", 
	"CN - Hong Kong", "Czech", "Czech Rep", "Czech Repub", "Czech Republic", 
	"Denmark", "Dubai", "Egypt", "Fance", "Finland", "France", "Germany", "Ghana", 
	"HK", "Hong Kong", "Hong Kong", "CN", "Hong Kong/Onsite", "HongKong", "Hungary", 
	"ID - Indonesia", "IN - Bangalore", "IN - New Delhi", "India", "India - Pune", "India/Sandy", 
	"India/Singapore", "India/UK", "Indonesia", "Israel", "Isreal", "Italy", "Japan", "Kenya", 
	"Korea", "KR - Seoul", "Kuwait", "Lithuania", "Madrid", "Malaysia", 
	"Malaysia/ONSITE", "Malaysia/Onsite", "Malyasia", "Manila", "Mayalsia", 
	"Morocco", "Moscow", "MY - Selangor", "Netherland", "Netherlands", "New Delhi", 
	"New Zealand", "Nigeria", "Norway", "NZ - Auckland", "Onsite", "ONSITE", "ONSITE-China", 
	"Onsite-Malaysia", "ONSITE/China", "Palestine", "PH - Manila", "Philipines", "Philippine", 
	"Philippines", "Philippines/Onsite", "Poland", "Portugal", "Prague", "Pune", "QA-Belgium", 
	"QA-Korea", "QA-UK", "Qatar", "Riyadh", "Russia", "SG - Singapore", "Shanghai", "Singapore", 
	"Singapore/Cecilia", "Slovakia", "South Africa", "Spain", "Sweden", "Switzerland", "Taiwan",
	"Taipei", "Tanzania", "TH - Bangkok", "Thailand", "Turkey", "TW - Taipei", "TW-Taipei", 
	"UAT", "Uganda", "UK", "Warsaw", "Warsaw/Poland", "LATAM", "NAM"};
	public final static SimpleDateFormat DateFormat = new SimpleDateFormat("MMM dd, yyyy");
	public final static SimpleDateFormat sdfFileNamePrefix = new SimpleDateFormat("MM-dd-yyyy");
	//public static HashMap<String, String> hmAbbr = new HashMap<String, String>();
	
	public static void loadBooked(){
		
	}
	
	public static void reloadData(eDataSections section){
		
		LoadCourseMatrix lcm = new LoadCourseMatrix();
		ConfigManager lst = new ConfigManager();
		LoadVAMatrix lvam = new LoadVAMatrix();
		DicT ldict = new DicT();
		
		Thread tConfig = new Thread(lst);
		Thread tCourseMatrix = new Thread(lcm);
		Thread tVAMatrix = new Thread(lvam);
		Thread tLdict = new Thread(ldict);
		
		
		switch (section)
		{
		case CourseMatrix:
			ConfigManager.saveProperties();
			if (mConfigOption.get(eDataSections.CourseMatrix.toString()) != null){
				lcm.setWorkbookName(mConfigOption.get(eDataSections.CourseMatrix.toString()));
			}
			tCourseMatrix.start();
			while (tCourseMatrix.isAlive()){
				//step ++;
				try {
					tCourseMatrix.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			break;
		case VAMatrix:
			ConfigManager.saveProperties();
			File cacheFile = new File("PBDataSheet.xlsx");
			if (mConfigOption.get(eDataSections.VAMatrix.toString()) != null){
				lvam.setWorkbookName(mConfigOption.get(eDataSections.VAMatrix.toString()));
			}
			tVAMatrix.start();
			while (tVAMatrix.isAlive()){
				try {
					tVAMatrix.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		case PBTemplate:
			ConfigManager.saveProperties();
			break;
		case Dictionary:
			ConfigManager.saveProperties();
			if (mConfigOption.get(eDataSections.Dictionary.toString()) != null){
				DicT.dictFile = mConfigOption.get(eDataSections.Dictionary.toString());
			}
			tLdict.start();
			while(tLdict.isAlive()){
				try {
					tLdict.join();
				} catch (InterruptedException e){
					e.printStackTrace();
				}
			}

			break;
		case NAM:
			ConfigManager.saveProperties();
			if (mConfigOption.get(eDataSections.NAM.toString()) != null){
				String sNAMFileName = mConfigOption.get(eDataSections.NAM.toString());
				sNAMFileName = new File(sNAMFileName).getName();
				dToday = getDateValueInFileName(sNAMFileName);
			}
			break;
		case ROW:
			ConfigManager.saveProperties();
			if (mConfigOption.get(eDataSections.ROW.toString()) != null)
			{
				String sNAMFileName = mConfigOption.get(eDataSections.ROW.toString());
				sNAMFileName = new File(sNAMFileName).getName();
				dToday = getDateValueInFileName(sNAMFileName);
			}

			break;
		case Configuration:
			tConfig.start();
			while (tConfig.isAlive()){
				try {
					tConfig.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		default:
			break;
		}
	}
	
	public static void savePBFile(String pbFileName){
		reloadData(eDataSections.PBTemplate);
		LoadPBTemplate lpbt = new LoadPBTemplate();
		Thread tPBTemplate = new Thread(lpbt);
		lpbt.setWorkBookName(mConfigOption.get(eDataSections.PBTemplate.toString()));
		lpbt.setBookingSheetName(pbFileName);
		
		tPBTemplate.start();
		while (tPBTemplate.isAlive())
		{
			try {
				tPBTemplate.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static boolean loadPBFile(String pbFileName){
		LoadPBTemplate lpbt = new LoadPBTemplate();
		Thread tPBTemplate = new Thread(lpbt);
		//lpbt.setWorkBookName(mConfigOption.get(eDataSections.PBTemplate.toString()));
		lpbt.setBookingSheetName(pbFileName);
		lpbt.setRead();
		tPBTemplate.start();
		while (tPBTemplate.isAlive())
		{			
			try {
				tPBTemplate.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return lpbt.isLoadSuccess;
	}
	
	public static Date getDateValueInFileName(String fileName){
		Pattern p;					
		Matcher m;
		//Date format: yyyymmdd
		p = Pattern.compile("^([0-9]{4})(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])");
		Calendar cal = Calendar.getInstance();
		Date d = cal.getTime();				
		m = p.matcher(fileName);
		while (m.find()){
			cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(m.group(3)));
			//JANUARY equals 0, so ...
			cal.set(Calendar.MONTH,Integer.parseInt(m.group(2)) - 1);
			cal.set(Calendar.YEAR,Integer.parseInt(m.group(1)));
		}
		return cal.getTime();
	}
	
	public static void generateIPSheet(Date dStart)
	{
		if (!vIP.isEmpty())
		{
			vIP.removeAllElements();
		}
		reloadData(eDataSections.Configuration);
		LoadInstructorPack liprow = new LoadInstructorPack();
		LoadInstructorPack lipnam = new LoadInstructorPack();
		Thread tIProw = new Thread(liprow);
		Thread tIPnam = new Thread(lipnam);
		liprow.setInput(mConfigOption.get(eDataSections.ROW.toString()), 1);
		liprow.setDate(dStart);
		lipnam.setInput(mConfigOption.get(eDataSections.NAM.toString()), 2);
		lipnam.setDate(dStart);
		tIPnam.start();
		tIProw.start();
		while (tIPnam.isAlive())
		{
			try {
				tIPnam.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		while (tIProw.isAlive())
		{
			try {
				tIProw.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		vIP.addAll(liprow.nwClasses);
		vIP.addAll(lipnam.nwClasses);
	}
	
	public static boolean copyFile(String inFile, String outFile) {	
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new FileInputStream(inFile);
			out = new FileOutputStream(outFile);
			int length = in.available();
			int len = (length % 10240 == 0) ? (length / 10240)
			: (length / 10240 + 1);
			byte[] temp = new byte[10240];
			for (int i = 0; i < len; i++) {
			in.read(temp);
			out.write(temp);
			}
		} catch (java.io.IOException e){
			e.printStackTrace();
			return false;
		} finally {
			if (in != null)
				try {
					in.close();
					return true;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
			if (out != null)
				try {
					out.close();
					return true;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
		}
		return false;
	}
	
	public static void debug(int line, Object msg){
		StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
		StackTraceElement e = stacktrace[2];//maybe this number needs to be corrected
		String methodName = e.getMethodName();
		System.out.println("Debug from " + methodName + msg);
	}

}



